package com.huizhonglm.diap.presenters.shop;

import com.alibaba.fastjson.JSON;
import com.bruce.common.utils.EmptyUtil;
import com.huizhonglm.diap.api.API;
import com.huizhonglm.diap.api.CommonResponse;
import com.huizhonglm.diap.api.HttpCallback;
import com.huizhonglm.diap.api.Status;
import com.huizhonglm.diap.api.model.shop.CancelCollectParams;
import com.huizhonglm.diap.api.model.shop.CollectShopParams;
import com.huizhonglm.diap.api.model.shop.ShopDetailParams;
import com.huizhonglm.diap.api.model.shop.ShopDetailResponse;
import com.huizhonglm.diap.core.utils.UserUtil;
import com.huizhonglm.diap.ui.shop.iView.ShopDetailIView;

/**
 * @author LiXiaoLong
 * @version 1.0
 * @since 2014-11-19 11:25
 */
public class ShopDetailPresenter {

    private ShopDetailIView iView;

    public ShopDetailPresenter(ShopDetailIView iView) {
        this.iView = iView;
    }

    public void loadDetail(String shopId){
        ShopDetailParams params = new ShopDetailParams();
        if (UserUtil.isLogin()) {
            params.userId = UserUtil.getUserId();
        }
        params.shopId = shopId;
        API.http().post("shopDetail", params, new HttpCallback() {
            @Override
            public void onSuccess(String result) {
                if (!EmptyUtil.isEmpty(result)) {
                    ShopDetailResponse resp = JSON.parseObject(result, ShopDetailResponse.class);
                    iView.onLoadDetailSuccess(resp);
                } else {
                    iView.onLoadDetailFailure();
                }
            }

            @Override
            public void onFailure(Throwable ex) {
                iView.onLoadDetailFailure();
            }
        });
    }

    public void collectShop(String shopId){
        CollectShopParams params = new CollectShopParams();
        params.userId = UserUtil.getUserId();
        params.shopId = shopId;
        API.http().post("collectShop", params, new HttpCallback() {
            @Override
            public void onSuccess(String result) {
                if (!EmptyUtil.isEmpty(result)) {
                    CommonResponse resp = JSON.parseObject(result, CommonResponse.class);
                    if (resp != null && Status.SUCCESS.equals(resp.getStatus())) {
                        iView.onCollectSuccess();
                    }
                } else {
                    iView.onCollectFailure();
                }
            }

            @Override
            public void onFailure(Throwable ex) {
                iView.onCollectFailure();
            }
        });
    }

    public void cancelCollect(String shopId){
        CancelCollectParams params = new CancelCollectParams();
        params.userId = UserUtil.getUserId();
        params.shopId = shopId;
        API.http().post("cancelCollect", params, new HttpCallback() {
            @Override
            public void onSuccess(String result) {
                if (!EmptyUtil.isEmpty(result)) {
                    CommonResponse resp = JSON.parseObject(result, CommonResponse.class);
                    if (resp != null && Status.SUCCESS.equals(resp.getStatus())) {
                        iView.onCancelSuccess();
                    }
                } else {
                    iView.onCancelFailure();
                }
            }

            @Override
            public void onFailure(Throwable ex) {
                iView.onCancelFailure();
            }
        });
    }
}
